package com.ruoyi.funding.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.funding.domain.FundingSource;
import com.ruoyi.funding.service.IFundingSourceService;
import com.ruoyi.system.api.RemoteProjectResearchService;
import com.ruoyi.system.api.domain.ProjectResearch;
import com.ruoyi.system.api.domain.Researcherinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.funding.domain.FundingResearch;
import com.ruoyi.funding.service.IFundingResearchService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 科研经费Controller
 * 
 * @author ycl
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/research")
public class FundingResearchController extends BaseController
{
    @Autowired
    private IFundingResearchService fundingResearchService;

    @Autowired
    private IFundingSourceService fundingSourceService;

    @Autowired
    private RemoteProjectResearchService remoteProjectResearchService;

    /**
     * 查询科研经费列表
     */
    @RequiresPermissions("funding:research:list")
    @GetMapping("/list")
    public TableDataInfo list(FundingResearch fundingResearch)
    {
        startPage();
        List<FundingResearch> list = fundingResearchService.selectFundingResearchList(fundingResearch);

        for(FundingResearch research : list){
            Long sourceId = research.getSourceId();
            FundingSource fundingSource = fundingSourceService.selectFundingSourceBySourceId(sourceId);
            if(fundingSource!=null)
                research.setSourceName(fundingSource.getSourceName());

            R<ProjectResearch> r = remoteProjectResearchService.getProjectInfo(research.getProjectId());
            ProjectResearch pr = r.getData();
            if(pr!=null) {
                research.setProjectName(pr.getProjectName());
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出科研经费列表
     */
    @RequiresPermissions("funding:research:export")
    @Log(title = "科研经费", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FundingResearch fundingResearch)
    {
        List<FundingResearch> list = fundingResearchService.selectFundingResearchList(fundingResearch);
        ExcelUtil<FundingResearch> util = new ExcelUtil<FundingResearch>(FundingResearch.class);
        util.exportExcel(response, list, "科研经费数据");
    }

    /**
     * 获取科研经费详细信息
     */
    @RequiresPermissions("funding:research:query")
    @GetMapping(value = "/{fundingId}")
    public AjaxResult getInfo(@PathVariable("fundingId") Long fundingId)
    {
        return success(fundingResearchService.selectFundingResearchByFundingId(fundingId));
    }

    /**
     * 新增科研经费
     */
    @RequiresPermissions("funding:research:add")
    @Log(title = "科研经费", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FundingResearch fundingResearch)
    {
        return toAjax(fundingResearchService.insertFundingResearch(fundingResearch));
    }

    /**
     * 修改科研经费
     */
    @RequiresPermissions("funding:research:edit")
    @Log(title = "科研经费", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FundingResearch fundingResearch)
    {
        return toAjax(fundingResearchService.updateFundingResearch(fundingResearch));
    }

    /**
     * 删除科研经费
     */
    @RequiresPermissions("funding:research:remove")
    @Log(title = "科研经费", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fundingIds}")
    public AjaxResult remove(@PathVariable Long[] fundingIds)
    {
        return toAjax(fundingResearchService.deleteFundingResearchByFundingIds(fundingIds));
    }
}
